package com.mycompony.shopping.shangguan.sys.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.mycompony.shopping.shangguan.sys.context.SessionUtil;
import com.mycompony.shopping.shangguan.sys.context.UserAuthContext;
import com.mycompony.shopping.shangguan.sys.model.Auth;
import com.mycompony.shopping.shangguan.sys.model.Menu;
import com.mycompony.shopping.shangguan.sys.model.User;
import com.mycompony.shopping.shangguan.sys.service.MenuService;
import com.mycompony.shopping.shangguan.sys.util.Constant;
import com.opensymphony.xwork2.ActionSupport;

public class MenuAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2423376536863493054L;

	private MenuService menuService;

	private List<Menu> menus;

	private Menu menu;

	private JSONArray rows;

	public List<Menu> getMenus() {
		return menus;
	}

	public void setMenus(List<Menu> menus) {
		this.menus = menus;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	public JSONArray getRows() {
		return rows;
	}

	public void setRows(JSONArray rows) {
		this.rows = rows;
	}

	public MenuService getMenuService() {
		return menuService;
	}

	public String getMenuList() throws Exception {

		User user = SessionUtil.getCurrentUser();

		List<Auth> authList = UserAuthContext.getAuthList(user);

		if (authList == null || authList.isEmpty()) {
			return ActionSupport.INPUT;
		}

		menus = menuService.queryLeftMenuList(authList);

		return ActionSupport.SUCCESS;
	}

	public String getAllMenu() throws Exception {
		menus = menuService.queryAllMenuList();

		rows = new JSONArray();
		JSONObject o = null;
		for (Menu menu : menus) {
			o = new JSONObject();
			o.put("menuid", menu.getMenuid());
			o.put("mename", menu.getMename());
			o.put("authid", menu.getAuthid());
			o.put("parent", menu.getParentid());
			// o.put("_parentId", menu.getParentid());
			rows.add(o);
		}

		return "root";
	}

	public String addMenu() throws Exception {

		menuService.addMenu(menu);

		returnActionResult();

		return null;
	}

	public String deleteMenu() throws Exception {

		menuService.deleteMenu(menu);

		returnActionResult();

		return null;
	}

	public String updateMenu() throws Exception {

		menuService.updateMenu(menu);

		returnActionResult();

		return null;
	}

	private void returnActionResult() throws IOException {

		HttpServletResponse response = ServletActionContext.getResponse();

		response.setCharacterEncoding("utf-8");

		PrintWriter out = response.getWriter();

		out.print(Constant.action_success);

		out.flush();

		out.close();
	}

}
